package com.cyk.comicforumjava.demos.intercept;

import com.cyk.comicforumjava.demos.mapper.LoginMapper;
import com.cyk.comicforumjava.demos.mapper.ManageMapper;
import com.cyk.comicforumjava.demos.properties.JwtTokenProperties;
import com.cyk.comicforumjava.demos.utils.JwtUtils;
import com.cyk.comicforumjava.demos.utils.ThreadLocalUtil;

import com.cyk.comicforumjava.demos.web.ManageController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 管理端拦截器
 */
@Slf4j
@Component
public class JwtTokenInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtTokenProperties jwtTokenProperties;
    @Autowired
    ManageMapper manageMapper;


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("管理端拦截器开始");
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }

        String token = request.getHeader("token");

//        if (token == null) {
//            response.setStatus(401);
//            return false;
//        }
        // 验证token
//        if(request.getRequestURI().startsWith("/user")){
//            return true;
//        }

        try{
            if(token==null|| "".equals(token)){
                System.out.println("token为");
                response.setStatus(403);
                System.out.println(request.getRequestURI());
                return false;
            }
            else if("token".equals(token)&&!request.getRequestURI().startsWith("/posts")){
                System.out.println("token为空");
                response.setStatus(403);
                return false;

            }
            else{
                System.out.println("token:"+token);
                Map<String, Object> map = JwtUtils.parseJWT(jwtTokenProperties.getSecretKey(),token );
                ThreadLocalUtil.set((Long) map.get("id"));
                Long id=ThreadLocalUtil.get();
                id=manageMapper.selectManagerById(id);
                //如果id为null并且请求地址为/Manage/**则返回flase设置状态403
                if(id==null&&request.getRequestURI().startsWith("/Manage")){
                    response.setStatus(403);
                    return false;
                }else{
                    System.out.println("用户id："+ThreadLocalUtil.get());
                    return true;
                }


            }

        }catch (Exception e){
            response.setStatus(403);
            throw e;
        }

    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadLocalUtil.remove(); //其中调用的就是 userThreadLocal.remove();
    }

}